package com.sl.au.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.Chance;
import com.sl.au.entity.ChanceStatistics;
import com.sl.au.entity.ExpectedOrder;
import com.sl.au.entity.ExpectedOrderAnalysis;
import com.sl.au.entity.ExpectedOrderToChance;
import com.sl.au.repository.ExpectedOrderRepository;
import com.sl.au.service.ExpectedOrderService;
import com.sl.au.service.QueryService;

@Service("ExpectedOrderService")
@Transactional
public class ExpectedOrderServiceImpl extends BaseServiceImpl implements ExpectedOrderService {

	@Autowired
	ExpectedOrderRepository expectedOrderRepository;
	
	@Autowired
	QueryService queryService;
	
	@PersistenceContext
	private EntityManager em;

	public ExpectedOrderRepository getRepository() {
		return this.expectedOrderRepository;
	}

	@Override
	public List<ExpectedOrder> getExpectedOrderByTime(int start, int length, Map<String, String> condition, String orderName,
			String dir) {
		String begin = condition.get("begin");
		String end = condition.get("end");
		begin += " 00:00:00";
		end += " 23:59:59";
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");         
		Date beginDate = null;
		Date endDate = null;
		try {
			beginDate = format.parse(begin);
			endDate = format.parse(end);
		} catch (ParseException e) {}   
		
		Page<ExpectedOrder> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "id");
		//权限控制
		if (condition.get("dataFilter").equals("所有"))
			pages = expectedOrderRepository.findAllByTime(beginDate, endDate, pageable);
		else if (condition.get("dataFilter").equals("本部门"))
			pages = expectedOrderRepository.findSuperiorByTime(beginDate, endDate, condition.get("superior"), pageable);
		else if (condition.get("dataFilter").equals("本片区"))
			pages = expectedOrderRepository.finAreaByTime(beginDate, endDate, condition.get("area"),pageable);
		else//个人
			pages = expectedOrderRepository.findSelfByTime(beginDate, endDate, condition.get("userId"), pageable);
		
		return pages.getContent();
	}
	
	@Override
	public List<ExpectedOrderAnalysis> getExpectedOrderForAnalysis(int start, int length, Map<String, String> condition, String orderName,
			String dir) {
		String month = condition.get("month");
		
		Query query = this.em.createNativeQuery("{call ExpectedOrderAnalysis(?)}",ExpectedOrderAnalysis.class);
		query.setParameter(1, month);
		
		@SuppressWarnings("unchecked")
		List<ExpectedOrderAnalysis> analysis = query.getResultList();
		
		return analysis;
	}


	@Override
	public boolean deleteExpectedOrderCount(List<String> list, String userId) {
		boolean result = false;
		if(list != null && list.size() > 0) {
			for(String orderId : list) {
				ExpectedOrder expectedOrder = expectedOrderRepository.findOne(orderId);
				if(userId.equals(expectedOrder.getUser().getId())) {
					expectedOrderRepository.delete(expectedOrder);
				}else {
					expectedOrder = null;
					result = true;
				}
			}
		}
		
		return result;
	}


}
